import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import { constPinia, piniaStore } from "@/store";
// 通过Vite的import.meta.glob()方法实现自动化导入路由
const store = constPinia(piniaStore);
// 子路由
export const childRoutes = [
  //选择城市
  {
    path: "/home",
    name: "Home",
    component: () => import("@/views/home/index.vue"),
  },

  //搜索食物
  {
    path: "/search",
    name: "Search",
    component: () => import("@/views/search/index.vue"),
  },
  //订单
  {
    path: "/order/",
    name: "Order",
    component: () => import("@/views/order/index.vue"),
    children:[
      {
        path:'orderDetail/:orderId',
        name:'OrderDetail',
        props:true,
        component: () => import('@/views/order/children/orderDetail/index.vue')
      }
    ]
  },
  //搜索位置
  {
    path: "/city/:id",
    name: "City",
    props: true,
    component: () => import("@/views/city/index.vue"),
  },
  //商家列表
  {
    path: "/msite",
    name: "Msite",
    meta: { keepAlive: true, address: true },
    component: () => import("@/views/msite/index.vue"),
  },
  //食物分类
  {
    path: "/category",
    props(route) {
      return {
        id: route.query.id,
        headTitle: route.query.headTitle,
      };
    },
    name: "Category",
    meta: { address: true },
    component: () => import("@/views/category/index.vue"),
  },
  //商店详情
  {
    path: "/shop",
    name: "Shop",
    props(route) {
      return { id: route.query.id };
    },
    component: () => import("@/views/shop/index.vue"),
  },
  //确认下单页面
  {
    path: "/confirmOrder/:shopId",
    name: "ConfirmOrder",
    props:true,
    component: () => import("@/views/confirmOrder/index.vue"),
    children: [
      //选择地址页面
      {
        path: "selectAddress",
        name: "SelectAddress",
        component: () => import("@/views/confirmOrder/children/selectAddress/index.vue"),
      },
      //订单备注信息
      {
        path: "remark/:mark_id",
        name: "Remark",
        props:true,
        component: () => import("@/views/confirmOrder/children/remark/index.vue"),
      },
    ],
  },

  //个人主页
  {
    path: "/profile",
    name: "Profile",
    meta: {
      keepAlive: true,
    },
    component: () => import("@/views/profile/index.vue"),
    children: [
      {
        path: "info",
        name: "Info",
        component: () => import("@/views/profile/children/info/index.vue"),
        children: [
          {
            path: "editAddress",
            name: "EditAddress",
            component: () => import("@/views/profile/children/info/children/editAddress/index.vue"),
            children: [
              {
                path: "addAddress",
                name: "AddAddress",
                component: () =>
                  import(
                    "@/views/profile/children/info/children/editAddress/children/addAddress/index.vue"
                  ),
                children: [
                  {
                    path: "searchAddress",
                    name: "SearchAddress",
                    component: () =>
                      import(
                        "@/views/profile/children/info/children/editAddress/children/addAddress/children/searchAddress/index.vue"
                      ),
                  },
                ],
              },
            ],
          },
          {
            path: "uploadAvatar",
            name: "UploadAvatar",
            component: () =>
              import("@/views/profile/children/info/children/uploadAvatar/index.vue"),
          },
        ],
      },
    ],
  },
  //登录界面
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/login/index.vue"),
  },
  //忘记密码
  {
    path: "/forget",
    name: "Forget",
    component: () => import("@/views/forget/index.vue"),
  },
];

// 根路由
const rootRoutes = [
  {
    path: "/",
    name: "layout",
    redirect: "/msite",
    component: () => import("@/layout/index.vue"),
    children: childRoutes,
  },
];
const routes: Array<RouteRecordRaw> = rootRoutes;

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  if (to?.meta?.address) {
  if (!store.latitude) {
    next({ path: "/home", replace: true });
  } else {
  NProgress.start();
  next();
  }
  } else {
    NProgress.start();
    next();
  }
});

router.afterEach((from, to) => {
  NProgress.done();
});

export default router;
